package Teste;

import Dao.ImpUsuarioDao;
import Model.Usuario;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {

    private static final ImpUsuarioDao usuarioDao = new ImpUsuarioDao();
    private static String username;
    private static String password;
    private static int doces;
    private static int clique;
    private static long id;

    private boolean loggedIn;

    @ManagedProperty(value = "#{navigationBean}")
    private NavigationBean navigationBean;

    public String doLogin() {
      
        for (Usuario u : usuarioDao.list()) {

            if (u.getApelido().equals(username) && u.getSenha().equals(password)) {
                loggedIn = true;
                this.doces = u.getDoces();
                this.clique = u.getClique();
                this.id = u.getId();
                return navigationBean.redirectToJogo();
            }
        }

        FacesMessage msg = new FacesMessage("Login error!", "ERROR MSG");
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        FacesContext.getCurrentInstance().addMessage(null, msg);

        return navigationBean.toLogin();

    }

    public static long getId() {
        return id;
    }

    public static void setId(long id) {
        LoginBean.id = id;
    }

    public void setDoces(int doces) {
        LoginBean.doces = doces;
    }

    public  int getClique() {
        return clique;
    }

    public void setClique(int clique) {
        LoginBean.clique = clique;
    }

    public static ImpUsuarioDao getUsuarioDao() {
        return usuarioDao;
    }

    public  int getDoces() {
        return doces;
    }

    public NavigationBean getNavigationBean() {
        return navigationBean;
    }

    public String doLogout() {

        loggedIn = false;

        FacesMessage msg = new FacesMessage("Logout success!", "INFO MSG");
        msg.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext.getCurrentInstance().addMessage(null, msg);

        return navigationBean.toSaindo();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
    }

}
